
package com.xyq.fs.views;

import java.io.File;

import java.util.concurrent.atomic.AtomicInteger;

import com.xyq.fs.base.BaseBootUp;
import com.xyq.fs.base.MyDirectoryS;

import com.xyq.fs.constants.IndexStringConstants;
import com.xyq.fs.contants.filesuffixes2.ExcelEnums;
import com.xyq.fs.contants.filesuffixes2.PdfEnums;
import com.xyq.fs.contants.filesuffixes2.PptEnums;
import com.xyq.fs.contants.filesuffixes2.TxtEnums;
import com.xyq.fs.contants.filesuffixes2.WordEnums;

import com.xyq.fs.scan.ScanFileInf;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.ImageCursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;

import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;

import javafx.scene.layout.Background;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class WindwosA extends Application {

	static {
			if (!BaseBootUp.bindPort()) {
				MyAlert.alertError("搜索工具已启动!!!");
				System.exit(0);
			}
	}

	public static Text tsB = new Text("");
	// 请输入有效的关键词
	public static Text tsc = new Text("");
	public static Button scanBtn;

	public static synchronized Button getScanBtn() {

		if (scanBtn != null)
			return scanBtn;
		else
			scanBtn = new Button("扫描文件");
		return scanBtn;
	}

	public static AtomicInteger FILE_TYPE = new AtomicInteger();
	public static RadioButton cb1;
	public static RadioButton cb2;
	public static ProgressIndicator pin;

	public static synchronized ProgressIndicator getPin() {

		if (pin == null)
			pin = new ProgressIndicator();
		return pin;
	}

	public static synchronized RadioButton getCb1() {

		if (cb1 == null)
			cb1 = new RadioButton("" + "全新扫描  ");
		return cb1;
	}

	public static synchronized RadioButton getCb2() {

		if (cb2 == null) {
			cb2 = new RadioButton("" + "精简模式  ");
			cb2.setSelected(true);
		}
		return cb2;
	}

	public static synchronized TextField getnoSearchTextField() {

		if (noSearchTextField == null)
			noSearchTextField = new TextField();
		return noSearchTextField;
	}

	public static synchronized ComboBox<String> getcomboBox() {

		if (comboBox == null)
			comboBox = new ComboBox<String>(options);
		return comboBox;
	}

	public static synchronized ComboBox<String> getcomboBox2() {

		if (comboBox2 == null)
			comboBox2 = new ComboBox<String>(options2);
		return comboBox2;
	}

	public static synchronized TextField getsearchKeyTextField() {

		if (searchKeyTextField == null)
			searchKeyTextField = new TextField();
		return searchKeyTextField;
	}

	public static synchronized AtomicInteger getSLEEP_NOW() {

		if (SLEEP_NOW == null)
			SLEEP_NOW = new AtomicInteger();
		return SLEEP_NOW;
	}

	public static TextField noSearchTextField;
	private static ObservableList<String> options = FXCollections.observableArrayList("文本类型");

	public static ComboBox<String> comboBox2;
	private static ObservableList<String> options2 = FXCollections.observableArrayList(IndexStringConstants.SYPF);

	public static ComboBox<String> comboBox;

	// 搜索框
	public static TextField searchKeyTextField;

	// 扫描暂停按钮 0从未开始，1表示扫描中，2暂停中
	public static AtomicInteger SLEEP_NOW;
	private static Background bk2 = WindowsHelp.getImgUrl(1);

	@Override
	public void start(Stage primaryStage) throws Exception {

		primaryStage.setResizable(false);
		GridPane grid = new GridPane();
		grid.setBackground(bk2);

		primaryStage.getIcons().add(WindowsHelp.getImg());

		// 标题和背景图片加载
		primaryStage.setTitle("文件内容搜索工具 v4.081");

		// -------------------------------------------------------------------------------------
		// 页面布局,表单布局
		grid.setAlignment(Pos.CENTER);
		grid.setHgap(10);
		grid.setVgap(10);
		grid.setPadding(new Insets(80, 29, 25, 25));
		Scene scene = new Scene(grid, 567, 335);
		ImageCursor Mouse = new ImageCursor(WindowsHelp.getsb(), 0, 0);
		// 在scene中修改光标
		scene.setCursor(Mouse);
		primaryStage.setScene(scene);

		// 关键词`
		Label userName = new Label("关键词");
		userName.setPrefWidth(100);
		grid.add(userName, 0, 1);

		getsearchKeyTextField().setId("txt");
		getsearchKeyTextField().setPrefWidth(100);
		getsearchKeyTextField().setPrefHeight(30);
		grid.add(getsearchKeyTextField(), 1, 1);
		scene.getStylesheets().add(WindwosA.class.getResource("WindowsA.css").toExternalForm());

		// 搜索按钮
		Button searchBtn = new Button("搜 索");
		searchBtn.setPrefSize(85, 31);
		grid.add(searchBtn, 2, 1);

		// -----------------------------------------------------------------------------------------

		// 过滤词
		// 关键词
		Label nosearchName = new Label("过滤词 ");
		nosearchName.setPrefWidth(100);
		grid.add(nosearchName, 0, 2);

		// 不搜索框

		getnoSearchTextField().setId("txt");
		getnoSearchTextField().setPrefWidth(330);
		getnoSearchTextField().setPrefHeight(30);
		grid.add(getnoSearchTextField(), 1, 2);

		// 清楚按钮
		Button cleanBtn = new Button("清 除");
		cleanBtn.setPrefSize(85, 31);
		grid.add(cleanBtn, 2, 2);

		// ---------------------------------------------------------------------------------
		Label fileTypeName = new Label("检索条件");
		fileTypeName.setPrefWidth(100);
		grid.add(fileTypeName, 0, 3);

		getcomboBox().setPrefSize(130, 28);
		getcomboBox().setValue(IndexStringConstants.QBLX);
		getcomboBox().setId("cb1");

		getcomboBox2().setPrefSize(130, 28);
		getcomboBox2().setValue(IndexStringConstants.SYPF);
		getcomboBox2().setId("cb1");

		GridPane grid3 = new GridPane();
		grid.add(grid3, 1, 3);
		grid3.add(getcomboBox(), 0, 0);
		grid3.add(getcomboBox2(), 1, 0);
		tsc.setId("scanTs");

		grid.add(tsc, 1, 0);
		// ---------------------------------------
		// 扫描按钮
		GridPane grid2 = new GridPane();

		getScanBtn().setPrefSize(130, 20);
		grid.add(grid2, 1, 4);
		grid2.add(getScanBtn(), 0, 0);

		getPin().setPrefSize(35, 35);
		getPin().setVisible(false);
		grid2.add(getPin(), 2, 0);

		getCb1().setId("cb2");
		getCb2().setId("cb2");

		grid2.add(getCb1(), 3, 0);
		grid2.add(getCb2(), 4, 0);

		// 提示词--------------------------------------
		tsB.setId("scanTs");
		grid.add(tsB, 1, 5);
		primaryStage.show();
		options.addAll(TxtEnums.getValues());
		options.addAll(WordEnums.getValues());
		options.addAll(ExcelEnums.getValues());
		options.addAll(PptEnums.getValues());
		options.addAll(PdfEnums.PDF);
		options.addAll(IndexStringConstants.QBLX);
		options.addAll(IndexStringConstants.QT);
		File[] roots = File.listRoots();
		for (File drive : roots) {

			options2.addAll((drive.toString().charAt(0) + "").toUpperCase());
		}
		MyDirectoryS.init();
		// --------------方法区--------------------------------

		/**
		 * 点击扫描按钮
		 */
		getScanBtn().setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

				ScanFileInf sf = new ScanFileInf();
				// 扫描暂停
				if (getSLEEP_NOW().get() == 1) {
					getSLEEP_NOW().set(2);
					getScanBtn().setText("扫描暂停中");
					getPin().setVisible(false);
				} else if (getSLEEP_NOW().get() == 0) {
					tsB.setText("");
					new Thread(new Runnable() {
						public void run() {
							sf.scan(getcomboBox().getValue());
						}
					}).start();
					getScanBtn().setText("扫描中");
					getSLEEP_NOW().set(1);
					getPin().setVisible(true);
				} else if (SLEEP_NOW.get() == 2) {
					getSLEEP_NOW().set(1);
					getScanBtn().setText("扫描中");
					getPin().setVisible(true);
				}
			}
		});

		/**
		 * 清除按钮点击事件
		 */
		cleanBtn.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

				getsearchKeyTextField().setText("");
				getnoSearchTextField().setText("");
				tsc.setText("");
				if (getSLEEP_NOW().get() == 0) {
					tsB.setText("");
					getcomboBox().setValue(IndexStringConstants.QBLX);
				}
			}
		});

		/**
		 * 搜索按钮点击事件
		 */
		searchBtn.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				search();
			}
		});

		getCb2().setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

			}
		});

		getnoSearchTextField().setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

				search();
			}
		});

		getsearchKeyTextField().setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				search();
			}
		});

		/**
		 * 窗口关闭事件
		 */
		primaryStage.setOnCloseRequest(e -> {

			primaryStage.close();
			getSLEEP_NOW().set(4);

			try {
				MyDirectoryS.writer.commit();

			} catch (Exception e1) {
			} finally {
				MyDirectoryS.closeAll();
				System.exit(0);
			}
		});
		comboBox.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

			}
		});
		comboBox2.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

			}
		});

		cb1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

			}
		});
	}

	public static void search() {

		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				String key = getsearchKeyTextField().getText().trim();
				if ("".equals(key) || key.length() == 0) {
					tsc.setText("关键词不能为空");
				} else {
					try {
						new WindowsB(true).start(new Stage());
					} catch (Exception e) {
						e.printStackTrace();
					}
					tsc.setText("");
				}
			}
		});
	}

	public static void main(String[] args) {

		launch(args);

	}

}
